# -*-Makefile-*-
# common rules for library building
# do not use this file in other than library sources subdirs

ACLOCAL_AMFLAGS = -I m4

SUFFIXES	= .scm .defs
# Makefile naming schema

# BOOT		REQUIRED	OPTIONAL	AUTO		EXTENSION
#
# -		DEFS		EXTRA_DEFS	-		.defs
# BOOT_SCM	SCM	EXTRA_SCM	AUTO_SCM	.scm
# -		C		EXTRA_C		AUTO_C		.c
# -		OBJS		EXTRA_OBJS	-		.o

#=== convenience variabless ===========================================

BOOT		= $(BOOT_SCM) $(BOOT_C)
ALL_DEFS	= $(DEFS_SRC) $(EXTRA_DEFS_F)

# all .scm files excluding boot files
ALL_SCM		= $(SCM) $(EXTRA_SCM_F) $(ALL_DEFS)

# all .scm files incuding boot files
ALL_ALL_SCM	= $(ALL_SCM) $(BOOT_SCM)

# all C files
ALL_C		= $(ALL_SCM) $(C) $(EXTRA_C) $(BOOT_C)

#all object files
ALL_OBJ		= $(ALL_C) evprim

#======================================================================
# all sources which are to go to the distribution
# Usage:
# libmylib_a_SOURCES = $(lib_SOURCES)

lib_SOURCES = $(DEFS_SRC:%=%.defs) $(EXTRA_DEFS:%=%.defs)	\
	$(BOOT_SCM:%=%.scm) $(BOOT_C:%=%.c)			\
	$(SCM:%=%.scm) $(EXTRA_SCM:%=%.scm)			\
	$(C:%=%.c) $(EXTRA_C:%=%.c)

#======================================================================
# Objects to build static library
# All required sources compiled + extra objects
# Usage:
# libmylib_a_LIBADD = $(lib_OBJECTS)
# libmylib_a_DEPENDENCIES = $(lib_OBJECTS)

lib_OBJECTS	  = $(ALL_OBJ:%=%.o)

#======================================================================
# Objects to build libtool shared library (.lo)
# All required sources compiled + extra objects
# Usage:
# libmylib_a_LIBADD = $(lib_LTOBJECTS)
# libmylib_a_DEPENDENCIES = $(lib_LTOBJECTS)

lib_LTOBJECTS	  = $(ALL_OBJ:%=%.lo)

#======================================================================
# Other bigloo-lib libraries required
# Usage:
# USE_LIBS = common node

INCLUDE_AND_LINK_DEFS	=						 \
	$(VPATH:%=-I %) $(VPATH:%=-L %) $(USE_LIBS:%=-I ./$(VPATH)/../%) \
	$(USE_LIBS:%=-L ../../%) $(USE_LIBS:%=-L ../%)

DEFS_COMMON = @DEFS@ -I. $(VPATH:%=-I %) -I.. $(USE_LIBS:%=-I ./$(VPATH)/../%) \
	-I@bigloo_lib_dir@
#	 -I$(includedir) 

#======================================================================
# afile for the library
# all the .scm files may (and possibly should) be used
# really it depends on $(SCM_FILES)
.afile: Makefile $(ALL_ALL_SCM:%=%.scm) evprim.scm
	LD_LIBRARY_PATH=@bigloo_lib_dir@ @afile@ $(VPATH:%=-I %) \
	-I . -o .afile $(ALL_ALL_SCM:%=%.scm) evprim.scm

#======================================================================
# files that may be safely cleaned in distribution: all the C files,
# produced from .scm (Note: boot files compile directly to .o)
CLEANFILES_COMMON = *.a *.la .afile $(ALL_DEFS:%=%.scm) $(ALL_ALL_SCM:%=%.c)	\
	$(EXTRA_PROGRAMS) driver evprim.scm make-lib.scm 		\
	evprim.c driver.c *.heap core *~

#======================================================================
# files that require extra tool to generate
MAINTAINERCLEANFILES = Makefile.in texinfo.tex mdate-sh version.texi \
	driver.scm

#======================================================================
# test executable

BIGLOO_LIBS	= -L@bigloo_lib_dir@ -l@bigloo_lib@ -l@bigloo_libgc@ \
		$(BIGLOO_USER_LIB)

TOOL_LIBS	= lib$(SUBPACKAGE)$(TOOL_LIB_SUFFIX) \
	$(USE_LIBS:%=../%/lib*$(TOOL_LIB_SUFFIX)) $(BIGLOO_LIBS)
# -lthread


#======================================================================
TESTS_ENVIRONMENT = ./driver $(VPATH:%=-I %) -I .
TESTS		= test.scm
#======================================================================
# compilation rules

%.c: %.scm .afile
	$(bigloo) $(BIGLOO_OPTIMIZATION_FLAGS) -afile .afile -cgen $< -o $@

.c.lo:
	$(LIBTOOL) --mode=compile $(COMPILE) -c $<

.c.o:
	$(COMPILE) -c $<

.defs.scm:
	LD_LIBRARY_PATH=@bigloo_lib_dir@  @cgen@ $(CGEN_LOCAL_OPTIONS) \
	$(VPATH:%=-I %) -I . $< -o $@

$(SUBPACKAGE).heap: $(ALL_ALL_SCM:%=%.scm) make-lib.scm .afile
	$(bigloo) -unsafe -q -mkaddheap -mkaddlib -heap-library $(SUBPACKAGE) make-lib.scm -addheap $@

# 	$(bigloo) -unsafe -mkaddheap -mkaddlib \
# 	 -afile .afile make-lib.scm -addheap $@

#======================================================================
evprim.scm: Makefile
	echo "(module $(SUBPACKAGE)-evprim				\
	(import $(ALL_ALL_SCM))					\
	(export *$(SUBPACKAGE)-version*)				\
	${USE_LIBS:%=(library %)}				\
	(eval(export-all))					\
	)							\
	(define *$(SUBPACKAGE)-version* \"$(VERSION)\")" | @bpp@ > $@

# neither "-g" nor "-O" have any meaning for this file, so we inline
# the $(COMPILE) without the $(CFLAGS) here
evprim.lo: evprim.c
	$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) \
	$(CPPFLAGS) $(AM_CFLAGS) -c evprim.c

evprim.o: evprim.c
	$(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \
	$(AM_CFLAGS) $(AM_CFLAGS) -c evprim.c

make-lib.scm: Makefile
	echo "(module make-lib			\
	(import $(ALL_ALL_SCM) $(SUBPACKAGE)-evprim)	\
	${USE_LIBS:%=(library %)}		\
	(eval(export-all))			\
	)" | @bpp@ > $@
#======================================================================
# params set by configure
LDFLAGS			= @LDFLAGS@
BIGLOO_USER_LIB		= @bigloo_user_lib@
bigloo			= @bigloo_executable@ -I . -L . $(INCLUDE_AND_LINK_DEFS)
BIGLOO_LIBRARY_SUFFIX   = @bigloo_library_suffix@
BIGLOO_OPTIMIZATION_FLAGS = @bigloo_optimization_flags@
lib_LIBRARIES		= @lib_LIBRARIES_target@
TOOL_LIB_SUFFIX           = @tool_lib_suffix@
#======================================================================
# these definitions make automake happy

transform=s,^driver$$,bigloo-$(SUBPACKAGE),

driver.scm:
	@bigloo_executable@ -i ../make-driver $(SUBPACKAGE) $(USE_LIBS) > foo && mv foo $@

#======================================================================
# bigloo-common as rpm

rpm: $(PACKAGE).spec.in
	./configure
	$(MAKE) $(AM_MAKEFLAGS) dist
	mv $(distdir).tar.gz /usr/src/redhat/SOURCES
	rpmbuild -bb $(PACKAGE).spec

# last line

